package com.wangxiang.sys.manage.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangxiang.sys.manage.dao.MenuListDao;
import com.wangxiang.sys.manage.entity.MenuListEntity;
import com.wangxiang.sys.manage.service.MenuListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service("menuListService")
public class MenuListServiceImpl extends ServiceImpl<MenuListDao, MenuListEntity> implements MenuListService {
    @Autowired
    MenuListDao menuListDao;

    @Override
    public List<MenuListEntity> getMenuList(int roleId)  {
        //获取该roleId拥有的菜单权限列表信息（菜单，按钮）,返回一个menuId集合，泛型int
        List<Integer> menuId = menuListDao.getMenuIdByRoleId(roleId);
        List<MenuListEntity> menuList = new ArrayList<>();
        //组合后的菜单
//        List<menuListEntity> menu = new ArrayList<>();

        //遍历该menuId集合
        for (int i = 0; i < menuId.size(); i++) {
            MenuListEntity menuListEntity = new MenuListEntity();
            menuListEntity = menuListDao.getMenuListByMenuId(menuId.get(i));
            menuList.add(menuListEntity);
        }
        //过滤，组合菜单
        List<MenuListEntity> menu = menuList.stream().filter((res) -> {
//            System.out.println("过滤=====》"+res);
            return res.getParentId() == 0;
        }).map((menumap) ->{
            menumap.setChildren(childrenMenu(menumap,menuList));
            return menumap;
        }).collect(Collectors.toList());


        return menu;
    }


    public List<MenuListEntity> childrenMenu(MenuListEntity parentMenu, List<MenuListEntity> all) {

        List<MenuListEntity> childrenMenu = new ArrayList<>();
        for (int i = 0; i<all.size();i++){
            if (all.get(i).getParentId() == parentMenu.getMenuId()){
                childrenMenu.add(all.get(i));
            }

        }


         return childrenMenu;
    }

    /**
     * 查所有菜单数据
     * @return
     */
    @Override
    public List<MenuListEntity> getAllMenu() {
        List<MenuListEntity> menuList = menuListDao.getAllMenuList();
        return menuList;
    }
}
